package com.example.androidframework.ui.course

import android.content.Context
import com.example.androidframework.ui.course.adapter.TextViewHolder
import com.example.androidframework.ui.course.coroutine.CoroutineActivity
import com.example.androidframework.ui.course.dialog.CommentInputDialogView
import com.example.androidframework.ui.course.flow.FlowActivity
import com.example.androidframework.ui.course.pageindex.PageIndexActivity
import com.example.androidframework.ui.course.pkblood.PkBloodActivity
import com.example.androidframework.ui.course.seriesback.PageAActivity
import com.example.androidframework.ui.course.thread.ThreadTestActivity
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.interfaces.OnSelectListener
import com.martin.lib_base.base.BaseViewModel
import com.martin.lib_base.bean.PreviewBean
import com.martin.lib_base.ktx.loge
import com.martin.lib_base.ktx.logi
import com.martin.lib_base.ktx.normalToast
import com.martin.lib_base.ui.PreviewActivity
import com.martin.lib_base.utils.MediaUtil

class CourseViewModel : BaseViewModel() {
    val viewHolders = listOf(
        TextViewHolder::class.java
    )

    // 评论输入框
    private val commentInputDialog by lazy {
        CommentInputDialogView()
    }

    //列表数据
    val itemList = listOf(
        "评论输入弹窗",
        "弹幕",
        "Radius",
        "BindingAdapter",
        "Shape",
        "Fragment 点击穿透",
        "PageIndex",
        "PK血条",
        "点赞跟随",
        "flow",
        "Preview",
        "选择图片",
        "线程通信方式",
        "Coroutine-协程",
        "连续多个页面返回"
    )

    /**
     * 条目点击事件
     */
    fun onItemClick(context: Context, position: Int) {
        loge("onItemClick title = ${itemList[position]}")
        when (itemList[position]) {
            "评论输入弹窗" -> commentInputDialog.show()
//            "弹幕" -> DanMuActivity.start(context)
//            "Radius" -> RadiusActivity.start(context)
//            "BindingAdapter" -> BindingAdapterActivity.start(context)
//            "Shape" -> ShapeActivity.start(context)
//            "Fragment 点击穿透" -> PenetrateActivity.start(context)
            "PageIndex" -> PageIndexActivity.start(context)
            "PK血条" -> PkBloodActivity.start(context)
//            "点赞跟随" -> LikeFollowActivity.start(context)

            "flow" -> FlowActivity.start(context)

            "Preview" -> PreviewActivity.start(context, listOf(
                "https://th.bing.com/th/id/R.82ece114307f62dc0b8aa2c366b9ef37?rik=GwmaljjvGJ75zg&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fwallpaper%2f1308%2f15%2fc2%2f24494094_1376530595238.jpg&ehk=L%2fNVdWJDsmRvIsmKuuUcvCGnK4DJwpxad4JswscWIN0%3d&risl=&pid=ImgRaw&r=0",
                "https://th.bing.com/th/id/R.82ece114307f62dc0b8aa2c366b9ef37?rik=GwmaljjvGJ75zg&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fwallpaper%2f1308%2f15%2fc2%2f24494094_1376530595238.jpg&ehk=L%2fNVdWJDsmRvIsmKuuUcvCGnK4DJwpxad4JswscWIN0%3d&risl=&pid=ImgRaw&r=0",
                "https://th.bing.com/th/id/R.82ece114307f62dc0b8aa2c366b9ef37?rik=GwmaljjvGJ75zg&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fwallpaper%2f1308%2f15%2fc2%2f24494094_1376530595238.jpg&ehk=L%2fNVdWJDsmRvIsmKuuUcvCGnK4DJwpxad4JswscWIN0%3d&risl=&pid=ImgRaw&r=0",
            ).map { PreviewBean(it) })

            "选择图片" -> showImageSelectBottomSheetView()

            "线程通信方式" -> ThreadTestActivity.start(context)

            "Coroutine-协程" -> CoroutineActivity.start(context)

            "连续多个页面返回" -> PageAActivity.start(context)

            else -> normalToast(itemList[position])
        }
    }

    // 选择图片方式底部弹出框
    private fun showImageSelectBottomSheetView() {
        XPopup.Builder(context).asBottomList(
            "请选择图片获取方式",
            listOf("拍照", "从相册获取").toTypedArray()
        ) { position, text ->
            when (position) {
                // 拍照
                0 -> MediaUtil.takeImage(context) { list ->
                    val pathList = list.map {
                        logi("拍照-选择的图片地址为: ${it.realPath}")
                        it.realPath
                    }
                    logi("拍照-选择的图片地址数组为: $pathList")
                }

                // 从相册选择
                else -> MediaUtil.pickImage(context, 1, false) { list ->
                    val pathList = list.map {
                        logi("从相册选择-选择的图片地址为: ${it.realPath}")
                        it.realPath
                    }
                    logi("从相册选择-选择的图片地址数组为: $pathList")
                }
            }
        }.show()
    }
}